/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.gui;



import com.home.common.ApplicationCommon;
import com.home.dao.AccountDao;
import com.home.mapping.Account;
import com.home.mapping.Agent;
import javax.swing.JOptionPane;

/**
 *
 * @author vi
 */
public class ChangePassword extends javax.swing.JInternalFrame {
    
    private MainScreen mainPanel;
    private Agent loginAgent;
    /**
     * Creates new form Login
     */
    public ChangePassword(MainScreen mainPanel) {
        initComponents();
        // set position center
        this.mainPanel = mainPanel;
        loginAgent = mainPanel.getAgentLogin();
        setBounds(ApplicationCommon.getRectangleForCenter(mainPanel.getSize().width, mainPanel.getSize().height, this.getWidth(), this.getHeight()));
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnChange = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        txtNewPass = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        txtConfirmPass = new javax.swing.JPasswordField();
        txtOldPass = new javax.swing.JPasswordField();

        setClosable(true);
        setTitle("Login");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(102, 102, 102));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Change Password");
        jLabel2.setToolTipText("");

        jLabel3.setText("Old Password");

        jLabel4.setText("New Password");

        btnChange.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_ok.png"))); // NOI18N
        btnChange.setText("Change");
        btnChange.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChangeActionPerformed(evt);
            }
        });

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_reset.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        txtNewPass.setToolTipText(null);
        txtNewPass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtNewPassKeyPressed(evt);
            }
        });

        jLabel5.setText("Confirm New Password");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 501, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtNewPass, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtConfirmPass)
                            .addComponent(txtOldPass, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnReset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnChange)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtOldPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtConfirmPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnReset)
                    .addComponent(btnChange))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        reset();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeActionPerformed
        if (validateInput()) {
            Account acc = mainPanel.getAccountLogin();
            String newPassword = String.valueOf(txtNewPass.getPassword());
            acc.setPassword(newPassword);
            
            boolean result = new AccountDao().saveOrUpdate(acc);
            if (result) {
                mainPanel.setAccountLogin(acc);
                JOptionPane.showMessageDialog(mainPanel, "Change Password successfully");
                this.dispose();
            }
        }
    }//GEN-LAST:event_btnChangeActionPerformed

    private void txtNewPassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNewPassKeyPressed
        
    }//GEN-LAST:event_txtNewPassKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChange;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField txtConfirmPass;
    private javax.swing.JPasswordField txtNewPass;
    private javax.swing.JPasswordField txtOldPass;
    // End of variables declaration//GEN-END:variables
    
    
    private void reset() {
        txtOldPass.requestFocus();
        txtOldPass.setText("");
        txtNewPass.setText("");
        txtConfirmPass.setText("");
    }
    
    private boolean validateInput() {
        String oldPassword = String.valueOf(txtOldPass.getPassword());
        String newPassword = String.valueOf(txtNewPass.getPassword());
        String confirmPassword = String.valueOf(txtConfirmPass.getPassword());
        if (oldPassword.trim().equals("")) {
            JOptionPane.showMessageDialog(mainPanel, "Please input : Old Password");
            return false;
        } else {
            if (!oldPassword.trim().equals(mainPanel.getAccountLogin().getPassword())) {
                JOptionPane.showMessageDialog(mainPanel, "Wrong old password");
                return false;
            }
        }
        if (newPassword.trim().equals("")) {
            JOptionPane.showMessageDialog(mainPanel, "Please input : New Password");
            return false;
        }
        if (!confirmPassword.trim().equals(newPassword.trim())) {
            JOptionPane.showMessageDialog(mainPanel, "New password not match ");
            return false;
        }
        return true;
    }
}
